const path = require('path')
const jsonServer = require('json-server')
const router = require('./router')
const db = require('./db')()
const server = jsonServer.create()
// const router = jsonServer.router(path.join(__dirname, 'db.json'));
const middlewares = jsonServer.defaults({
  static: path.join(__dirname, '../public'),
})

const PORT = 8000
server.use(middlewares)
// server.use(router);
server.use(jsonServer.bodyParser)
server.use((req, res, next) => {
  const json = res.json.bind(res)
  res.success = (data) => {
    json({
      code: 0,
      msg: '请求成功',
      data,
    })
  }
  res.fail = (msg,code=-1,data) => {
    json({
      code,
      msg,
      data
    })
  }
  next()
})
router(server)
const jsonRouter = jsonServer.router(db)
server.use('/api', jsonRouter)
server.listen(PORT, () => {
  console.log('JSON Server is running')
})
